package com.q.tqb.activity.mine;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;

import com.q.tqb.R;
import com.q.tqb.adapter.BaseFragmentPagerAdapter;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.fragment.TweeterFragment;
import com.q.tqb.fragment.TweeterShareFragment;
import com.q.tqb.util.DensityUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MineTweeterActivity extends BaseActivity {


    @BindView(R.id.tab_tweeter)
    TabLayout mTabTweeter;
    @BindView(R.id.vp_tweeter)
    ViewPager mVpTweeter;

    private BaseFragmentPagerAdapter mFragmentPagerAdapter;

    private TweeterShareFragment tweeterShareFragment;


    @Override
    public int getLayoutId() {
        return R.layout.activity_mine_tweeter;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle(getString(R.string.mine_tweeter));
        setTvRight(getString(R.string.string_share));

        List<Fragment> fragments = new ArrayList<>();
        tweeterShareFragment = TweeterShareFragment.newInstance();
        TweeterFragment tweeterFragment = TweeterFragment.newInstance();
        fragments.add(tweeterFragment);
        fragments.add(tweeterShareFragment);

        String[] tabStrings = {"推荐用户", "二 维 码"};
        mFragmentPagerAdapter = new BaseFragmentPagerAdapter(getSupportFragmentManager(), this, fragments, tabStrings);
        mTabTweeter.setTabMode(TabLayout.MODE_FIXED);
        LinearLayout linearLayout = (LinearLayout) mTabTweeter.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.divider));
        linearLayout.setDividerPadding(DensityUtil.dip2px(MineTweeterActivity.this, 10.0f)); //设置分割线间隔

        mVpTweeter.setAdapter(mFragmentPagerAdapter);
        mTabTweeter.setupWithViewPager(mVpTweeter);
        mTabTweeter.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(mTabTweeter, 40, 40);
            }
        });
    }


    @OnClick({R.id.iv_back, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right:
                tweeterShareFragment.share();
                break;
        }
    }

    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        assert llTab != null;
        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }

    }

}
